package ${domain}.module.wsys.mgr;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import ${domain}.frame.auth.Token;
import ${domain}.frame.base.ErrorType;
import ${domain}.frame.utils.IDgenerator;
import ${domain}.frame.utils.MapperUtil;
import ${domain}.frame.utils.Message;
import ${domain}.frame.utils.ValidationUtil;
import ${domain}.module.wsys.ent.Visitor;
import ${domain}.module.wsys.mpr.VisitorMapper;
import ${domain}.module.wsys.req.VisitorCreateRequest;
import ${domain}.module.wsys.req.VisitorDeleteRequest;
import ${domain}.module.wsys.req.VisitorFindRequest;
import ${domain}.module.wsys.req.VisitorUpdateRequest;
import ${domain}.module.wsys.rsp.VisitorCreateResponse;
import ${domain}.module.wsys.rsp.VisitorDeleteResponse;
import ${domain}.module.wsys.rsp.VisitorFindResponse;
import ${domain}.module.wsys.rsp.VisitorUpdateResponse;

/**
 * VISITOR - 应用接入
 *
 * @author wangbing
 * @version 0.0.1
 * @since 2020-06-25
 */
@Transactional
@Service
public class VisitorManagerImpl implements VisitorManager {

    @Autowired
    private VisitorMapper visitorMapper;

    /**
     * 插入
     *
     * @param request 请求对象
     * @param token   令牌
     * @return 响应
     */
    public VisitorCreateResponse create(VisitorCreateRequest request, Token token) {
        VisitorCreateResponse response = new VisitorCreateResponse();

        ValidationUtil.validate(request, response);
        if (response.hasError()) {
            return response;
        }

        {// 应用码唯一检查
            VisitorFindRequest visitorFindRequest = new VisitorFindRequest();
            visitorFindRequest.setAppKey(request.getAppKey());
            VisitorFindResponse visitorFindResponse = this.find(visitorFindRequest, token);
            if (visitorFindResponse.hasError()) {
                response.addErrors(visitorFindResponse.getErrors());
                return response;
            } else if (visitorFindResponse.getTotalCount() > 0) {
                response.addError(ErrorType.UNIQUENESS_ERROR, "[appKey]应用码已存在，请检查!");
                return response;
            }
        }

        long id = IDgenerator.nextId();
        Visitor entity = MapperUtil.map(request, Visitor.class);
        entity.setId(id);

        long result = visitorMapper.insert(entity, token);
        if (1L != result) {
            response.addError(ErrorType.BUSINESS_ERROR, Message.CREATE_FAILURE);
            return response;
        }
        response.setId(id);

        return response;
    }

    /**
     * 逻辑删除
     *
     * @param request 请求对象
     * @param token   令牌
     * @return 响应
     */
    public VisitorDeleteResponse delete(VisitorDeleteRequest request, Token token) {
        VisitorDeleteResponse response = new VisitorDeleteResponse();

        ValidationUtil.validate(request, response);
        if (response.hasError()) {
            return response;
        }

        long result = visitorMapper.delete(request.getId(), token);
        if (1L != result) {
            response.addError(ErrorType.BUSINESS_ERROR, Message.DELETE_FAILURE);
            return response;
        }
        response.setResult(result);

        return response;
    }

    /**
     * 更新
     *
     * @param request 请求对象
     * @param token   令牌
     * @return 响应
     */
    public VisitorUpdateResponse update(VisitorUpdateRequest request, Token token) {
        VisitorUpdateResponse response = new VisitorUpdateResponse();

        ValidationUtil.validate(request, response);
        if (response.hasError()) {
            return response;
        }

        Visitor entity = visitorMapper.getById(request.getId(), token);
        if (entity == null) {
            response.addError(ErrorType.BUSINESS_ERROR, Message.GET_FAILURE);
            return response;
        }

        MapperUtil.map(request, entity);
        long result = visitorMapper.update(entity, token);
        if (1L != result) {
            response.addError(ErrorType.BUSINESS_ERROR, Message.UPDATE_FAILURE);
            return response;
        }
        response.setResult(result);

        return response;
    }

    /**
     * 查询
     *
     * @param request 请求对象
     * @param token   令牌
     * @return 响应
     */
    @Transactional(readOnly = true)
    public VisitorFindResponse find(VisitorFindRequest request, Token token) {
        VisitorFindResponse response = new VisitorFindResponse();

        ValidationUtil.validate(request, response);
        if (response.hasError()) {
            return response;
        }

        if (request.getPageSize() != 0) {
            PageHelper.startPage(request.getPageNumber(), request.getPageSize());
        }
        if (StringUtil.isNotEmpty(request.getSortKey())) {
            PageHelper.orderBy(request.getSortKey() + " " + request.getSortType());
        }
        PageInfo<Visitor> pageInfo = new PageInfo<>(visitorMapper.find(request, token));

        response.setResult(pageInfo.getList());
        response.setTotalCount(pageInfo.getTotal());

        return response;
    }
}
